package com.app.miwawa.cui.system;


import org.apache.log4j.Logger;
import org.framework.common.cui.AbstractController;
import org.framework.common.ext.exception.CommonException;
import org.framework.common.ext.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.app.miwawa.api.system.entity.Admin;
import com.app.miwawa.api.system.entity.Center;
import com.app.miwawa.api.system.entity.center.Relation;
import com.app.miwawa.api.system.entity.view.Account;
import com.app.miwawa.api.system.service.bo.AccountBo;
import com.app.miwawa.api.system.service.bo.AdminBo;
import com.app.miwawa.api.system.service.bo.UsersBo;
import com.app.miwawa.cui.esm.ContractController;

/**
 * 早教门店控制器类
 * <p>描述: </p>
 * <p>Company: Professional</p>
 * @author chanlong(陈龙)
 * @date 2016年1月13日 下午3:30:30
 * @version 1.0.2016
 */
@RestController
public class UsersController extends AbstractController {

	@Autowired
	private UsersBo usersBo;
	
	@Autowired
	private AccountBo accountBo;
	
	@Autowired
	private AdminBo adminBo;
	
	private Logger log = Logger.getLogger(ContractController.class);
	
	@RequestMapping("/{dbname}/users/save")
	public Object save(@PathVariable final String dbname, @ModelAttribute final Relation entity) throws Exception {
		if(check(dbname)) return error("提交参数有误，请检查数据源或参数绑定源！");
		
		try{
			// 注册数据源
			registerDataSource(dbname);
			
			// 保存实体对象
			return usersBo.insert(entity).getUsers();
		} catch (Exception e) {
			return error("保存用户信息失败！",e);
		}
	}
	
	@RequestMapping("/{dbname}/users/update")
	public Object update(@PathVariable final String dbname, @ModelAttribute final Relation entity) throws Exception {
		if(check(dbname)) return error("提交参数有误，请检查数据源或参数绑定源！");
		
		try{
			// 注册数据源
			registerDataSource(dbname);
			
			// 保存实体对象
			usersBo.update(entity);
			
			return success("更新用户信息成功");
		} catch (Exception e) {
			return error("更新用户信息失败！",e);
		}
	}
	
	@RequestMapping("/{dbname}/users/password")
	public Object password(@PathVariable final String dbname, @ModelAttribute final Relation entity) throws Exception {
		if(check(dbname)) return error("提交参数有误，请检查数据源或参数绑定源！");
		
		try{
			// 注册数据源
			registerDataSource(dbname);
			
			// 保存实体对象
			usersBo.password(entity);
			
			return success("修改密码成功");
		} catch (Exception e) {
			return error("修改密码失败！",e);
		}
	}
	
	@RequestMapping("/{dbname}/admin/password")
	public Object adminPassword(@PathVariable final String dbname, @ModelAttribute final Admin entity) throws Exception {
		if(check(dbname)) return error("提交参数有误，请检查数据源或参数绑定源！");
		
		try{
			// 注册数据源
			registerDataSource(dbname);
			
			// 保存实体对象
			adminBo.password(entity);
			
			return success("修改密码成功");
		} catch (Exception e) {
			return error("修改密码失败！",e);
		}
	}
	
	@RequestMapping(value="/{dbname}/user/check/password")
	public Object checkUser(@PathVariable final String dbname, @ModelAttribute final Account entity) throws Exception {
		if(check(dbname)) return error("提交参数有误，请检查数据源或参数绑定源！");
		
		try{
			registerDataSource(dbname);
			
			Account account = entity.checkMobile(accountBo);
			
			if(account == null){
				return success("账户信息有误，登录失败！");
			}else if((account = entity.checkPassword(accountBo)) == null){
				return success("账户密码错误，登录失败！");
			}else{
				account.setAuths(accountBo);
				
				request.getSession().setAttribute(USER_LOGINED_SESSION, account);
				
				return success(account);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new CommonException(log, "登录系统失败！");
		}
	}
	
	@RequestMapping(value="/{dbname}/admin/check/password")
	public Object check(@PathVariable final String dbname, @ModelAttribute final Admin entity) throws Exception {
		if(check(dbname)) return error("提交参数有误，请检查数据源或参数绑定源！");
		
		try{
			registerDataSource(dbname);
			
			Admin admin = entity.checkAccount(adminBo);
			
			if(admin == null){
				return success("账户信息有误，登录失败！");
			}else if((admin = entity.checkPassword(adminBo)) == null){
				return success("账户密码错误，登录失败！");
			}else{
				String centerid = admin.getCenterid();
				if(StringUtil.isNotEmpty(centerid)){
					Center center = adminBo.getCenter(centerid);
					admin.setDbname(center.getDbname());
				}
				request.getSession().setAttribute(USER_LOGINED_SESSION, admin);
				
				return success(admin);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new CommonException(log, "登录系统失败！");
		}
	}
}
